<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/11/13
 * Time: 16:54
 * Created by Created by Panxsoft.
 */

namespace App\Models\Categories;


use App\Models\Language;
use App\Models\Products\Product;
use App\Services\MobileApp\AppEnv;
use App\Services\Utils\ImageHelper;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

/**
 * @property int $category_id
 * @property int $parent_id
 * @property int sort_order
 *
 * @property-read string $origin_image
 *
 * @property-read Category|Collection $subCategories
 * @property-read CategoryDescription $categoryDescription
 * @property-read string $image
 * @property-read string category_description_name - join category_description 表的 name 字段
 *
 * Class Category
 * @package App\Models\Categories
 * @mixin Builder
 */
class Category extends Model
{
    protected $guarded = [];

    protected $table = 'category';
    protected $primaryKey = 'category_id';

    const PARENT_ID = 0;

    const FILTER_TYPE_ALL = 'all';
    const FILTER_TYPE_TOP = 'top';
    const CATEGORY_STATUS_ON = 1;

    protected $casts = [
        'sort_order' => 'int',
    ];

    public function categoryDescription()
    {
        return $this->hasOne(CategoryDescription::class, 'category_id', 'category_id')
            ->where('language_id', AppEnv::languageId());
    }

    /**
     * @used-by $subCategories
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function subCategories()
    {
        return $this->hasMany(Category::class, 'parent_id', 'category_id')
            ->with(['subCategories', 'categoryDescription']);
    }

    public function products()
    {
        return $this->belongsToMany(Product::class, 'product_to_category', 'category_id', 'product_id');
    }

    public function categoryToStores()
    {
        return $this->hasMany(CategoryToStore::class, 'category_id', 'category_id');
    }

    /**
     * @used-by $origin_image
     * @return null|string
     * @throws \Exception
     */
    public function getOriginImageAttribute()
    {
        if (!$this->attributes['image']) return null;

        return ImageHelper::generateImagePath($this->attributes['image']);

    }

    /**
     * @used-by $image
     *
     * @return null|string
     * @throws \Exception
     */
    public function getImageAttribute()
    {
        if (!$this->attributes['image']) return null;

        return ImageHelper::generateImagePath($this->attributes['image']);
    }

    /**
     * @param array $category_ids
     * @return array
     */
    public static function getSubCategoryIds(array $category_ids)
    {
        $sub_category_ids = (new self)->where('status', Category::CATEGORY_STATUS_ON)
            ->whereIn('parent_id', $category_ids)
            ->pluck('category_id')
            ->all();

        if (count($sub_category_ids)) {
            $category_ids = array_merge($category_ids, self::getSubCategoryIds($sub_category_ids));
        }

        return $category_ids;
    }

}